package cn.itheima.servlet;

import cn.itheima.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/listStudent")
    public class listStudent extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            /*创建高效字符流读取文件数据*/
            BufferedReader br = new BufferedReader(new FileReader("D:\\stu.txt"));
            /*创建list集合存储读取的数据*/
            ArrayList<Student> list = new ArrayList<>();
            /*循环读写*/
            String line;
            while ((line=br.readLine())!=null){
                /*处理读取的数据*/
                String[] arr = line.split(",");
                /*根据数据创建学生对象*/
                Student stu = new Student(arr[0], Integer.valueOf(arr[1]), Integer.valueOf(arr[2]));
                list.add(stu);
            }
            /*释放资源*/
            br.close();
            /*获取会话域对象添加共享数据*/
            req.getSession().setAttribute("list",list);
            /*  req.getServletContext().setAttribute("list",list);*/
            /*重定向到查询界面*/
            resp.sendRedirect("/stu/liststudent.jsp");

        }
}
